/*
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 * 
 */

/* Last Edit:	Sep 1991.	for CanScan by UEKUSA.					*/

/* Modified hideously by Subrata K. Sircar for use with MultApp.	*/
/* My assumptions are that the application delegate will keep track	*/
/* of the string table, the app will load the appropriate nib in 	*/
/* in the XXX_main.m file and images are stored in the appropriate 	*/
/* directories.														*/

/* Known bugs:  the initialization each time of launchDir, etc. is	*/
/* wasteful, and if the user has no preference, systemLanguages		*/
/* will return NULL each time.  This should be an object with an	*/
/* init method, but that makes code much uglier...					*/

#import "Global.h"
#import <appkit/NXImage.h>
#import <appkit/NXBitmapImageRep.h>


#define NATIVE_LANGUAGE		"English"
		
static char					launchDir[MAXPATHLEN+1];
static id					stringSet		= nil;		
static char *const			*languages;


/* If the user has not set a language preference, load the nib
 * section stored in the Mach-O by default. Otherwise,
 * the nib file loaded would follow the language set in the Preferences
 * Application.
 */

id LoadLocalNib(const char *nibFile, id owner, BOOL withNames, NXZone *zone)
{
    BOOL found = NO;
    id retval = nil;
	char path[MAXPATHLEN+1];

	if (!launchDir || !*launchDir) {
		if (retval = [NXApp delegate]) {
			sprintf(launchDir,[retval launchDirectory]);
			retval = NULL;
		} else getAppDirectory(launchDir);
	}

    if (languages = [NXApp systemLanguages]) {
		while (!found && *languages) {
			sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, nibFile);
			if (!access(path, R_OK)) {
				retval = [NXApp loadNibFile:path owner:owner withNames:withNames fromZone:zone];
				found = YES;
			}
			if (!strcmp(*languages, NATIVE_LANGUAGE)) break;
			languages++;
		}
    }

    return found ? retval : [NXApp loadNibSection:nibFile owner:owner withNames:withNames fromZone:zone];
}

id LocalImageRep(const char *file, NXZone *zone)
{
    id retval = nil;
    char path[MAXPATHLEN+1];

	if (!launchDir || !*launchDir) {
		if (retval = [NXApp delegate]) {
			sprintf(launchDir,[retval launchDirectory]);
			retval = NULL;
		} else getAppDirectory(launchDir);
	}

    if (languages = [NXApp systemLanguages]) {
		while (!retval && *languages) {
			sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, file);
			if (!access(path, R_OK)) retval = [[NXBitmapImageRep allocFromZone:zone] initFromFile:path];
			if (!strcmp(*languages, NATIVE_LANGUAGE)) break;
			languages++;
		}
    }
    return retval ? retval : [[NXBitmapImageRep allocFromZone:zone] initFromSection:file];
}

id LocalImage(const char *file, NXZone *zone)
{
    id retval = nil;
    char path[MAXPATHLEN+1];

	if (!launchDir || !*launchDir) {
		if (retval = [NXApp delegate]) {
			sprintf(launchDir,[retval launchDirectory]);
			retval = NULL;
		} else getAppDirectory(launchDir);
	}

    if (languages = [NXApp systemLanguages]) {
		while (!retval && *languages) {
			sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, file);
			if (!access(path, R_OK)) retval = [[NXImage allocFromZone:zone] initFromFile:path];
			if (!strcmp(*languages, NATIVE_LANGUAGE)) break;
			languages++;
		}
    }
    return retval ? retval : [[NXImage allocFromZone:zone] initFromSection:file];
}

void LocalHelp(char *path, const char *file)
{
    id retval = NULL;

	if (!launchDir || !*launchDir) {
		if (retval = [NXApp delegate]) sprintf(launchDir,[retval launchDirectory]);
		else getAppDirectory(launchDir);
	}

    if (languages = [NXApp systemLanguages]) {
		while (*languages) {
			sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, file);
			if (!access(path, R_OK)) return;
	    	languages++;
		}
    }
    sprintf(path, "%s/%s.lproj/%s", launchDir, NATIVE_LANGUAGE, file);
}

char *LocalHelpFont()
{
    static char	EnglishFont[]  = "Helvetica";
    static char	JapaneseFont[] = "GothicBBBHelvetica";

	languages = [NXApp systemLanguages];

    if (languages && *languages && !strcmp(*languages, "Japanese")) return JapaneseFont;
    else return EnglishFont;
}

char *LocalString(char *string)
{
    char	*rtnString;

	if (!stringSet) stringSet = [[NXApp delegate] stringSet:nil];

	if (!stringSet) return string;
	rtnString = (char *)[stringSet valueForStringKey: string];
	return rtnString ? rtnString : string;
}

void getDateString(DAY_TIME *date, char *string)
{
    static int	order_type = -1;
    static char *date_form = NULL;
    static char *time_form = NULL;
    static char *nodata = NULL;

    if (date->year < 0) {
		if (nodata == NULL) nodata = LocalString("noDateData");
		strcpy(string, nodata);
    } else {
		char buf[MESS_MAX];
		if (order_type < 0) {
			char *order = LocalString("date_order");
			if (strncmp(order,"MDY",3) == 0) order_type = 1;
			else if (strncmp(order,"YMD",3) == 0) order_type = 2;
			else if (strncmp(order,"DMY",3) == 0) order_type = 3;
		}
		if (date_form == NULL) date_form = LocalString("date_form");

		switch(order_type) {
	    default:
	    case 1:
	        sprintf(buf, date_form, date->month, date->day, date->year);
			break;
	    case 2:
			sprintf(buf, date_form, date->year, date->month, date->day);
			break;
	    case 3:
			sprintf(buf, date_form, date->day, date->month, date->year);
			break;
		}

		if (date->hour > 0) {
			char buf2[MESS_MAX];
			if (time_form == NULL) time_form = LocalString("time_form");
			sprintf(buf2, time_form, date->hour ,date->minute ,date->second);
			strcpy(string, strcat(buf, buf2));
		} else strcpy(string, buf);
    }
}
